package cn.coldleaf.wiki.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;

/**
 * @datetime 2023/10/15 上午11:25
 * @user cold
 **/
@Slf4j
public class JsonUtil {
    /**
     * 对象转json
     * @datetime 2023-10-15 11:35:31
     * @user cold
     **/
    public static String toJson(Object obj){
        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            log.error("对象转json失败",e);
        }
        return null;
    }

    /**
     * json转对象
     * @datetime 2023-10-15 11:37:35
     * @user cold
     **/
    public static <T>T fromJson(String json,Class<T> tClass){
        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.readValue(json,tClass);
        } catch (JsonProcessingException e) {
            log.error("json转对象失败",e);
        }
        return null;
    }
}
